<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>微信登录</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="../css/index.css" />
    <link rel="stylesheet" type="text/css" href="../css/common.css" />
    <style type="text/css">
      body{-webkit-overflow-scrolling:touch;}
      html, body{overflow-x: hidden;padding: 0;margin: 0;}
      body{
        background: #ffffff;
      }
      .position_box{
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: url('../image/login/bg.png') no-repeat;
        background-size: contain;
        display: flex;
        flex-direction: column;
      }
      .login_logo{
        margin-top: 5.6rem;
      }
      .login_logo img{
        width: 9.3rem;
        margin: 0 auto;
      }
      .login_btn{
        margin-top: 16.6rem;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
      }
      .login_btn>div:nth-of-type(1){
        width: 13.3rem;
        height: 2.28rem;
        border-radius: 1.14rem;
        background: #fe4aa0;
        text-align: center;
        font-size: 0.85rem;
        color: #fff;
        letter-spacing: 2px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 0.66rem;
      }
      .login_btn>div:nth-of-type(1)>img{
        width: 1.6rem;
        display: inline-block;
        margin-right: 0.64rem;
      }
      .login_btn>p{
        font-size: .59rem;
        color: #666;
        margin-bottom: 0.64rem;
        text-align: center;
      }
      .login_btn>p>span{
        color: #FE4AA0;
      }
      .login_btn>div:nth-of-type(2){
        font-size: 0.59rem;
        color: #666;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 0.66rem;
      }
      .login_btn>div:nth-of-type(2)>img{
        width: 0.53rem;
        margin-right: 0.4rem;
      }
    </style>
</head>
<body>
  <div class="aui-content">
    <div class="position_box">
      <div class="login_logo">
        <img src="../image/login/logo.png" />
      </div>
      <div class="login_btn weChat_box">
        <div><img src="../image/login/weChat.png" onclick="wxauth();"/>微信登录</div>
        <p>登录注册即表示同意<span id="xiyi" onclick="openWeb(this)" data-url="" data-title="用户隐私协议" tapmode>《用户隐私协议》</span></p>
        <div onclick="openPublic(this)" data-frame="login_mobile_frm" data-title="手机登录" tapmode><img src="../image/login/phone.png" />手机号登录</div>
      </div>
  </div>
</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/zepto.min.js"></script>
<script type="text/javascript" src="./script/common.js"></script>
<script type="text/javascript">
    var wx;
    apiready = function(){
        api.parseTapmode();
        var header = $api.byId('header');
        $api.fixStatusBar(header);
        wx = api.require('wx');
        var systemType = api.systemType; // 比如： ios
        var appVersion = api.appVersion; // 比如： 1.0.0
        //获取微信登录配置
        _Ajax(window.APIServer.LoignCfg,'post','','json',function(ret){
            if (ret.code==1) {
                var wx = ret.data.wx;
                var apple = ret.data.apple;
                $('#xiyi').attr('data-url',ret.data.user_protocol_url);
                if (wx=='1') {
                    if (systemType=='ios') {
                        if (apple.is_open!='1') {
                            $('.mobile_login').hide();
                            $('.wx_login').show();
                            $('.mobile_login2').show();
                        }else{
                            if (appVersion!=apple.version) {
                                $('.mobile_login').hide();
                                $('.wx_login').show();
                                $('.mobile_login2').show();
                            }
                        }
                    }else{
                        $('.mobile_login').hide();
                        $('.wx_login').show();
                        $('.mobile_login2').show();
                    }
                }
            }
        });

    };
    //微信登录
    function wxauth(){
        //是否安装客户端
        wx.isInstalled(function(ret, err) {
            if (!ret.installed) {
                alert('您还没安装微信客户端');
                return false;
            }else{
                //step1 获取code...
                wx.auth({
                    apiKey: ''
                }, function(ret, err) {
                    //step get access_token
                    if (ret.status) {
                        _loading('登录中...','',true);
                        wx.getToken({
                            apiKey: '',
                            apiSecret: '',
                            code: ret.code
                        }, function(ret, err) {
                            if (ret.status) {
                                //step3 getuserinfo
                                wx.getUserInfo({
                                    accessToken: ret.accessToken,
                                    openId: ret.openId
                                }, function(ret, err) {
                                    if (ret.status) {
                                        //last 提交服务器验证
                                        var uinfo = ret;
                                        uinfo.device_model = api.deviceModel;
                                        uinfo.device_id = api.deviceId;
                                        _Ajax(window.APIServer.WxLogin,'post',{values:uinfo},'json',function(ret){
                                            _loading_close();
                                            //console.log(JSON.stringify(ret))
                                            if (ret.code==1) {
                                                var uinfo = ret.data;
                                                if (uinfo.is_bind_mobile==0) {
                                                    var frame = {
                                                        'title':'绑定手机',
                                                        'framename':'bind_mobile_frm',
                                                        'is_login_bind':1,
                                                        'token':uinfo.token,
                                                        'uid':uinfo.uid,
                                                        'uinfo':uinfo
                                                    }
                                                    openPublic(JSON.stringify(frame));
                                                }else{
                                                    //存储用户信息
                                                    _SetPrefs('token', uinfo.token);
                                                    _SetPrefs('uid', uinfo.uid);
                                                    _SetPrefs('uinfo', uinfo);
                                                    //广播事件：用户登录成功
                                                    _SendEvent('UserLogin', uinfo.uid);
                                                    _toast('登录成功！');
                                                    setTimeout(function() {
                                                         api.closeToWin({
                                                             name : 'root'
                                                         });
                                                    }, 500)
                                                }
                                            }else if (ret.code==2) {
                                                api.openWin({
                                                    name: 'invite_code_frm'
                                                    ,url: 'widget://html/invite_code_frm.html'//
                                                    ,allowEdit:true
                                                    ,pageParam: {
                                                        wxinfo: ret.data
                                                    }
                                                });
                                            }else{
                                                _toast(ret.msg);
                                            }

                                        });
                                    } else {
                                        _loading_close();
                                        _toast('获取信息失败');
                                    }
                                });
                            } else {
                                _loading_close();
                                _toast('登录失败');
                            }
                        });
                    } else {
                        var code_arr = {'-1':'未知错误','1':'您取消了登录','2':'您拒绝了登录','3':'微信未安装'};
                        _toast(code_arr[err.code]);
                    }
                });
            }
        });

    }
</script>
</html>
